<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\HasApiTokens;


class Consumer extends Model implements Authenticatable
{
    use HasApiTokens,Notifiable;
    protected $table = 'consumers';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nickname','avatar','contact_tele','sex','password','award_integration','follower_count','followed_count','twitters_count','status'
    ];

    public function findForPassport($contactTele){
        return Consumer::where('contact_tele',$contactTele)->first();
    }
    public function setPasswordAttribute($password)
    {
        if (Hash::needsRehash($password)) {
            $this->attributes['password'] = Hash::make($password);
        }

    }
    //用户的订单
    public function order(){
        return $this->hasMany(Orders::class);
    }
    //用户参加的活动
    public function activities(){
        return $this->belongsToMany(Activity::class,'activitiesjoin','consumer_id','activity_id')->withTimestamps();
    }
    //用户参与的培训
    public function trains(){
        return $this->belongsToMany(Train::class,'trainsjoin','consumer_id','train_id')->withTimestamps();
    }
    //参与奖品兑换清单
    public function awardlogs(){
        return $this->hasMany(Awardlog::class);
    }
    //用户所有收到的消息
    public function messages(){
        return $this->hasMany(Message::class);

    }
    //查看全部发布的晒图消息
    public function Twiwtters(){
        return $this->hasMany(Twitter::class);
    }
    //查看全部参与的回复
    public function comments(){
        return $this->hasManyThrough(Comment::class,'comment_consumer_id');
    }
    //查看用户晒图的所有回复
    public function commented(){
        return $this->hasManyThrough(Comment::class,Twitter::class,'consumer_id','twitter_id','id');
    }
    //查看全部的参与评论
    public function replies(){
        return $this->hasManyThrough(Reply::class,'reply_consumer_id');
    }
    //查看用户所有被回复的评论
    public function replied(){
        return $this->hasManyThrough(Reply::class,Comment::class,'comment_consumer_id','comment_id','id');
    }
    //获取用户关注的俱乐部
    public function clubs(){
        return $this->belongsToMany(Club::class,'clubfollow','followed_id')->withTimestamps();
    }
    //用户的关注
    public function following()
    {
        return $this->belongsToMany(self::class,'follow','follower_id','followed_id')->withTimestamps();
    }

//用户的粉丝
    public function followers()
    {
        return $this->belongsToMany(self::class,'follow','followed_id','follower_id')->withTimestamps();
    }

//关注用户
    public function followThisUser($consumer)
    {
        return $this->following()->toggle($consumer);
    }
    public function keytodata($keyname){

        return collect($keyname)->map(function($key){
            if(  !is_string($this->$key) && !is_int($this->$key)){

                if(  $this->$key  instanceof Carbon){
                    return [$key=>$this->$key->toFormattedDateString()];
                }
                if(  $key =='status'){
                    dd($this->$key);
                    if($this->$key == 1){
                        return [$key=>'禁用'];
                    }
                    else{
                        return [$key=>'使用中'];
                    }
                }
                else{
                    return [$key=>$this->$key->pluck('name')];
                }

            }

            return [$key=>$this->$key];
        })->collapse()->toArray();

    }

    /**
     * Get the name of the unique identifier for the user.
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        // TODO: Implement getAuthIdentifierName() method.
    }

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        // TODO: Implement getAuthIdentifier() method.
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        // TODO: Implement getAuthPassword() method.
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        // TODO: Implement getRememberToken() method.
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string $value
     * @return void
     */
    public function setRememberToken($value)
    {
        // TODO: Implement setRememberToken() method.
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        // TODO: Implement getRememberTokenName() method.
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        // TODO: Implement getJWTIdentifier() method.
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        // TODO: Implement getJWTCustomClaims() method.
        return [];
    }
}
